package kamkor.ann.namerecog.network.converter

import scala.collection.mutable.HashMap

/** Converts neural network output to String. 
 *  For example if 5th output neuron fires, "Polish" will be returned<br><br>
 * 
 *  4 -> "Polish", where 4 is an index of array, which value is 1<br><br>
 * 
 *  input[4] == 1, where input is {0,0,0,0,1}<br><br>
 * 
 * @author kamkor 
 */
class OutputConverter(indexConversionMap: HashMap[Int, String]) extends IConverter[Array[Double], String] {	
	def this() { this(new HashMap[Int, String]) }
	
	/**  Removes all elements from conversion map */
	def clearConversionMap() { indexConversionMap.clear() } 
	
	/** Adds index conversion
	 * 
	 * @param kv ie., when output[4] == 1, then 4 -> Polish
	 */
	def addConversion(kv: (Int, String)) { indexConversionMap += kv }
	
	/** Converts neural network output, to human understandable output.
	 *  ie. when {0,0,0,0,1} is neural network output, "Polish" will be returned, if 
	 *  mapping was 4 -> "Polish"  
	 * 
	 * @param i.e {0,0,0,0,1} If it has 5 elements, then converter also must have 5 conversions
	 * @return output ie. "Polish" or "" if there is no value close to 1 in it, or if
	 * there are more output elements than conversions 
	 */
	def convert(output: Array[Double]): String = {		
		val index = output.findIndexOf(_ > 0.6) 
		if (output.length == indexConversionMap.size && index != -1) {
			indexConversionMap(index)
		} else {
			""
		}
	}	
}